package org.test.client.ui.component.panel;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LayoutManager;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.Stroke;

import javax.swing.JPanel;

/**
 * 此面板 需重写 
 *  暂时 实现的 不好
 * @author Guodeqi
 *
 */
public class RoundPanel extends JPanel{
	private JPanel container = null;
	private String title = "";
	
	private String txtStyle = "Comic Sans MS";
	private String txtStyle2 = "宋体";
	private String txtStyle3 = Font.DIALOG;
	
	private int txtSize = 35;
	private Color lineColor = Color.GRAY;
	private Color shadowColor = new Color(173, 180, 186);
	private Color shadowColor2 = new Color(206, 205, 187);
//	private Color shadowColor = Color.black;
	private Color startColor = new Color(184, 197, 213);
	private Color startColor1 = new Color(180, 190, 210);
	private Color startColor2 = new Color(175, 185, 205);
	private Color endColor = null;
	
	private 	Font titleFont = new Font(txtStyle3, Font.PLAIN, txtSize-15);
	private int roundx = 10;
	private int roundy = 10;
	private int arcWidth = 15;
	private int arcHeight = 15;
	private int innerArcWidth = 15;
	private int innerArcHeight = 15;
	
	private boolean needInnerPanel = false;
	private double widthPercent = 0;
	private JPanel innerPanel = null;
	private int innerPanelPaddingX = 15;
	
	private Color commColor = this.getBackground();
	private Color mainPanelColor = commColor;
	private Color innerPanelColor = null;
	private Color titleColor = Color.black;
	
	
	public RoundPanel(){ 
		this("",false,0);
	}
	
	public RoundPanel(String title){ 
		this(title,false,0);
	}
	
	public RoundPanel(String title, boolean needInnerPanel,double wp){
		this.needInnerPanel = needInnerPanel;
		this.widthPercent = wp;
		this.title = title;
		
		endColor = new Color(commColor.getRed()-15, commColor.getGreen()-15, commColor.getBlue()-15);
		container = new JPanel();
		super.setLayout(null);
		super.add(container);
		
		if(needInnerPanel){
			innerPanelColor = commColor;
			mainPanelColor = endColor;
			innerPanel = new JPanel();

			super.add(innerPanel);
			
			innerPanel.setBackground(innerPanelColor);
		}
		container.setBackground(mainPanelColor);
	}
	
	
	@Override
	protected void paintComponent(Graphics g) {
		Dimension preDim = this.getPreferredSize();
		Dimension size = this.getSize();
		int w = Math.max(preDim.width, size.width);
		int h = Math.max(preDim.height, size.height);
		
		Graphics2D g2d = (Graphics2D)g;
		//设置线条颜色及抗锯齿
		g2d.setColor(lineColor);
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		Paint oldPaint = g2d.getPaint();

		//绘制圆角矩形
		g2d.drawRoundRect(roundx,roundy, w-roundx*2, h-roundy*2, arcWidth, arcHeight);
		g2d.setColor(mainPanelColor);
		g2d.fillRoundRect(roundx+1,roundy+1, w-roundx*2-2, h-roundy*2-2, arcWidth, arcHeight);
		
		//将顶部填充颜色
		GradientPaint newPaint = new GradientPaint(0, 0, startColor, w-roundx, 0, endColor);
		g2d.setPaint(newPaint);
		g2d.fillRoundRect(roundx+1, roundy+1, w-roundx*2-1, txtSize+arcHeight,arcWidth, arcHeight);

		//恢复paint 并且 画横线
		g2d.setPaint(oldPaint);
		int tmpx2 = w-roundx;
		if(needInnerPanel){
			tmpx2 = tmpx2 - 2*innerPanelPaddingX;
		}
		g2d.drawLine(roundx, roundy+txtSize, tmpx2, roundy+txtSize);
		//将标题 写入
		g2d.setFont(titleFont);
		g2d.setColor(titleColor );
		g2d.drawString(title, roundx+arcWidth+1, roundy+txtSize-8);
		
		//设置容器面板位置
		cx = roundx+1;
		cy = roundy+txtSize+1;
		if(!needInnerPanel){
			cw = w-roundx*2-1;
			ch = (h-arcHeight-arcWidth-txtSize-1);
		}else{
			drawInnerPanel(g2d,w,h);
		}
//		float[] dash = new float[]{2};
//		Stroke s = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 3.5f,dash,0f);
//		g2d.setStroke(s);

		//实现阴影效果
		int arcWidth2 = arcWidth/2+1;
		GradientPaint shadowGP1 = new GradientPaint(0, 0, startColor1, w-roundx, 0, Color.LIGHT_GRAY);
		g2d.setPaint(shadowGP1);
		g2d.drawLine(roundx+arcWidth2, roundy+1,(w-roundx-arcWidth2+4), roundy+1);
		GradientPaint shadowGP2 = new GradientPaint(0, 0, startColor2, w-roundx, 0, Color.LIGHT_GRAY);
		g2d.setPaint(shadowGP2);
		g2d.drawLine(roundx+arcWidth2-5, roundy+2,(w-roundx-arcWidth2)+4, roundy+2);
		
		container.setBounds(cx, cy, cw, ch);
		container.doLayout();
	}
	
	private void drawInnerPanel(Graphics2D g2d, int w, int h) {
		int width = (int) (widthPercent*(w-roundx*2-1));
		int height = (h-arcHeight-arcWidth-txtSize-1);
		int x = w-width - roundx - innerPanelPaddingX;
		int y = roundy + txtSize/2;
		g2d.setColor(lineColor);
		g2d.drawRoundRect(x+1, y, width, height, innerArcWidth, innerArcHeight);
		g2d.setColor(innerPanelColor);
		g2d.fillRoundRect(x+2, y+1, width-2, height-2, innerArcWidth, innerArcHeight);
		innerPanel.setBounds(x+2, y+innerArcHeight+1,width-2, height-2*innerArcHeight);
		innerPanel.doLayout();
		
		//画阴影线
		int arcWidth3 = innerArcWidth/2+1;
		GradientPaint shadowGP3 = new GradientPaint(0, 0, innerPanelColor, w-roundx, 0, Color.LIGHT_GRAY);
		g2d.setPaint(shadowGP3);
		g2d.drawLine(x+arcWidth3, y+1,(x+width-arcWidth3+3), y+1);
		GradientPaint shadowGP4 = new GradientPaint(0, 0, innerPanelColor, w-roundx, 0, Color.LIGHT_GRAY);
		g2d.setPaint(shadowGP4);
		g2d.drawLine(x+arcWidth3-3, y+2,(x+width-arcWidth3)+3, y+2);
		
		cw = x - roundx;
		ch = height;
	}

	int  cx = 0;
	int cy = 0;
	int cw = 0;
	int ch =0;
	
	@Override
	public void setLayout(LayoutManager layoutmanager) {
		if(container != null){
			container.setLayout(layoutmanager);
		}
	}
	
	@Override
	public Component add(Component component1) {
		Component add = container.add(component1);
		return add;
	}
	
	public Component addCompForInner(Component component1){
		if(innerPanel == null){
			return null;
		}
		return innerPanel.add(component1);
	}
	
	public void setLayoutForInner(LayoutManager layoutmanager){
		if(innerPanel != null){
			innerPanel.setLayout(layoutmanager);
		}
	}
	
	@Override
	public void setSize(int i, int j) {
		if(container != null){
			container.setSize(i,j-arcHeight-arcWidth-txtSize-1);
		}
		if(innerPanel!=null){
			innerPanel.setSize((int) (i*widthPercent), j);
		}
		super.setSize(i, j);
	}
	
	
}
